package net.maku.im.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.im.constant.ImConstant;
import net.maku.im.constant.ImRedisKey;
import net.maku.im.convert.ImUserConvert;
import net.maku.im.dao.ImUserDao;
import net.maku.im.dto.ImUserBanDTO;
import net.maku.im.dto.ImUserQueryDTO;
import net.maku.im.dto.ImUserUnbanDTO;
import net.maku.im.entity.ImUserEntity;
import net.maku.im.mq.ImRedisMQTemplate;
import net.maku.im.service.ImUserService;
import net.maku.im.vo.ImUserVO;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * IM用户
 *
 * @author Blue
 * @since 1.0.0 2024-07-14
 */
@Service
@RequiredArgsConstructor
@DS(ImConstant.DS_IM_PLATFORM)
public class ImUserServiceImpl extends BaseServiceImpl<ImUserDao, ImUserEntity> implements ImUserService {

    private final ImRedisMQTemplate redisMQTemplate;

    @Override
    public PageResult<ImUserVO> page(ImUserQueryDTO query) {
        LambdaQueryWrapper<ImUserEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotEmpty(query.getUserName()), ImUserEntity::getUserName, query.getUserName());
        wrapper.like(StrUtil.isNotEmpty(query.getNickName()), ImUserEntity::getNickName, query.getNickName());
        if (CollectionUtil.isNotEmpty(query.getCreatedTime()) && query.getCreatedTime().size() > 1) {
            wrapper.between(ImUserEntity::getCreatedTime, query.getCreatedTime().get(0), query.getCreatedTime().get(1));
        }
        wrapper.orderByDesc(ImUserEntity::getId);
        IPage<ImUserEntity> page = baseMapper.selectPage(getPage(query), wrapper);
        return new PageResult<>(ImUserConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    @Override
    public void ban(ImUserBanDTO dto) {
        LambdaUpdateWrapper<ImUserEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ImUserEntity::getId, dto.getId());
        wrapper.set(ImUserEntity::getIsBanned, true);
        wrapper.set(ImUserEntity::getReason, dto.getReason());
        this.update(wrapper);
        // 推送到处理队列，等待im-platfrom处理
        redisMQTemplate.opsForList().rightPush(ImRedisKey.IM_QUEUE_USER_BANNED, dto);
    }

    @Override
    public void unban(ImUserUnbanDTO dto) {
        LambdaUpdateWrapper<ImUserEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ImUserEntity::getId, dto.getId());
        wrapper.set(ImUserEntity::getIsBanned, false);
        wrapper.set(ImUserEntity::getReason, Strings.EMPTY);
        this.update(wrapper);
    }

    @Override
    public List<ImUserVO> findByName(String name) {
        // 取出用户名或昵称匹配的前10条
        LambdaQueryWrapper<ImUserEntity> queryWrapper = Wrappers.lambdaQuery();
        if(StrUtil.isNotEmpty(name)){
            queryWrapper.like(ImUserEntity::getUserName, name).or().like(ImUserEntity::getNickName, name);
        }
        queryWrapper.select(ImUserEntity::getId, ImUserEntity::getUserName);
        queryWrapper.orderByDesc(ImUserEntity::getId);
        queryWrapper.last("limit 10");
        List<ImUserEntity> users = this.list(queryWrapper);
        return ImUserConvert.INSTANCE.convertList(users);
    }

}